#ifndef __MYARRAY_H__
#define __MYARRAY_H__

#include <iostream>
using namespace std;

template <typename T>
class Array
{
private:
    T *data;      // 存储数组元素的指针
    int capacity; // 数组的总容量
    int size;     // 当前数组中的元素个数
public:
    // 构造函数，传入容量参数并初始化
    Array(int cap)
    {
        this->capacity = cap;
        this->size = 0;
        data = new T[this->capacity];
    }

    // 拷贝构造函数，实现深拷贝
    Array(const Array &other)
    {
        this->capacity = other.capacity;
        this->size = other.size;
        this->data = new T[this->capacity];
        for (int i = 0; i < this->size; i++)
        {
            this->data[i] = other.data[i];
        }
        
    }

    // 析构函数，释放堆内存
    ~Array()
    {
        if (this->data != NULL)
        {
            delete[] this->data;
            this->data = NULL;
            this->capacity = 0;
            this->size = 0;
        }
        
    }

    // 赋值运算符重载，实现深拷贝
    Array &operator=(const Array &other)
    {
        if (this->capacity != NULL)
        {
            delete[] this->data;
            this->capacity = 0;
            this->size = 0;
        }
        this->capacity = other.capacity;
        this->size = other.size;
        this->data = new T[this->capacity];
        for (int i = 0; i < this->size; i++)
        {
            this->data[i] = other.data[i];
        }
        return *this;
    }

    // 尾插法，添加元素到数组末尾
    void push_back(const T &value)
    {
        if (this->capacity == this->size)
        {
            return;
        }
        this->data[this->size++] = value;
    }

    // 尾删法，移除数组末尾的元素
    void pop_back()
    {
        if (this->size == 0)
        {
            return;
        }
        this->size--;
    }

    // 下标运算符重载，访问数组中的元素
    T &operator[](int index)
    {
        return this->data[index];
    }

    // 获取当前元素个数
    int getSize() const
    {
        return this->size;
    }
    
    // 获取数组的总容量
    int getCapacity() const
    {
        return this->capacity;
    }
};

#endif